package com.cszjzx.member.action;

import java.util.List;

import javax.servlet.http.HttpServletRequest;

import org.apache.struts2.ServletActionContext;

import com.cszjzx.member.po.Department;
import com.cszjzx.member.po.Field;
import com.cszjzx.member.po.Major;
import com.cszjzx.member.po.Master;
import com.cszjzx.member.po.Member;
import com.cszjzx.member.service.DepartmentServiceImpl;
import com.cszjzx.member.service.MemberServiceImpl;
import com.cszjzx.member.service.TempleMemberService;
import com.cszjzx.member.service.TempleMemberServiceImpl;
import com.opensymphony.xwork2.ActionSupport;

@SuppressWarnings("serial")
public class ImportStudentInfoByHandAction extends ActionSupport{
	private int process;
	private HttpServletRequest request = ServletActionContext.getRequest();
	private int checkcondition;
	private String name;
	private String idcard;
	private String department;
	private String major;
	private String message;
	private Member member;
	private List<Department> departments;
	private List<Major> majors;
	private TempleMemberService templeMemberService;
	private String[][] info;
	
	public String[][] getInfo() {
		return info;
	}

	public void setInfo(String[][] info) {
		this.info = info;
	}

	public List<Department> getDepartments() {
		return departments;
	}

	public void setDepartments(List<Department> departments) {
		this.departments = departments;
	}

	public List<Major> getMajors() {
		return majors;
	}

	public void setMajors(List<Major> majors) {
		this.majors = majors;
	}

	public Member getMember() {
		return member;
	}

	public void setMember(Member member) {
		this.member = member;
	}

	public String getMessage() {
		return message;
	}

	public void setMessage(String message) {
		this.message = message;
	}

	public String getName() {
		return name;
	}

	public void setName(String name) {
		this.name = name;
	}

	public String getIdcard() {
		return idcard;
	}

	public void setIdcard(String idcard) {
		this.idcard = idcard;
	}

	public String getDepartment() {
		return department;
	}

	public void setDepartment(String department) {
		this.department = department;
	}

	public String getMajor() {
		return major;
	}

	public void setMajor(String major) {
		this.major = major;
	}

	public int getCheckcondition() {
		return checkcondition;
	}

	public void setCheckcondition(int checkcondition) {
		this.checkcondition = checkcondition;
	}

	public int getProcess() {
		return process;
	}

	public void setProcess(int process) {
		this.process = process;
	}
	
	public String show() throws Exception{
		departments = new DepartmentServiceImpl().getDepartments();
		return "showSuccess";
	}
	
	@SuppressWarnings("unchecked")
	public String execute(){
		
		if(idcard == null || idcard.equals("")){
			message = "所填信息不能为空";
			return "executeError";
		}
		if(name == null || name.equals("")){
			message = "所填信息不能为空";
			return "executeError";
		}
		if(department == null || department.equals("")){
			message = "所填信息不能为空";
			return "executeError";
		}
		if(major == null || major.equals("")){
			message = "所填信息不能为空";
			return "executeError";
		}
		String[] fieldsName;
		String[] fieldsValue;
		int size=0;
		List<Field> fields = (List<Field>) request.getSession().getAttribute("fields");
		//size代表相应阶段所需要的字段信息
		for(int i=0;i<fields.size();i++){
			if(fields.get(i).getStage() == process){
				size++;
			}
		}
		fieldsName = new String[size];	//字段的name
		fieldsValue = new String[size];	//字段的value
		if(checkcondition == 0){
			//为老团员时
			if(process == -1){
				size = fields.size();
				fieldsName = new String[size];
				fieldsValue = new String[size];
				for(int i=0,j=0;i<fields.size();i++){
					fieldsName[j] = fields.get(i).getName();
					String s = request.getParameter(fieldsName[j]);
					fieldsValue[j] = s;
					j++;
				}
			}
			else{
				for(int i=0,j=0;i<fields.size() && j<size;i++){
					if(fields.get(i).getStage() == process){
						fieldsName[j] = fields.get(i).getName();
						String s = request.getParameter(fieldsName[j]);
						fieldsValue[j] = s;
						j++;
					}
				}
			}
		}
		else {
			//为老团员时
			if(process == -1){
				size = fields.size();
				fieldsName = new String[size];
				fieldsValue = new String[size];
				for(int i=0,j=0;i<fields.size() && j<size-2;i++){
					//不是‘备注’和'转出时间'字段
					if((!(fields.get(i).getName().equals("remark")))&&(!(fields.get(i).getName().equals("outtime")))){
						fieldsName[j] = fields.get(i).getName();
						String s = request.getParameter(fieldsName[j]);
						if(!fieldsName[j].equals("age")){
							if(s == null || s.equals("")){
								message = "所填信息不能为空";
								return "executeError";
							}
							if(fields.get(i).getDefaultValue() != null){
								String ss = fields.get(i).getDefaultValue();
								if(!ss.equals(s)){
									message = "所填信息不符合默认值要求";
									return "executeError";
								}
							}
						}
						fieldsValue[j] = s;
						j++;
					}
				}
				fieldsName[size-2] = "remark";
				fieldsValue[size-2] = request.getParameter("remark");
				fieldsName[size-1] = "outtime";
				fieldsValue[size-1] = request.getParameter("outtime");
			}
			else{
				for(int i=0,j=0;i<fields.size() && j<size;i++){
					if(fields.get(i).getStage() == process){
						fieldsName[j] = fields.get(i).getName();
						String s = request.getParameter(fieldsName[j]);
						if(!fieldsName[j].equals("age")){
							if(s == null || s.equals("")){
								message = "所填信息不能为空";
								return "executeError";
							}
						}
						fieldsValue[j] = s;
						j++;
					}
				}
			}
		}
		Master master = (Master)request.getSession().getAttribute("master");
		templeMemberService = new TempleMemberServiceImpl();
		int operationType;
		if(process == 1 || process == -1){
			operationType = 0;
		}
		else{
			operationType = 1;
		}
		boolean flag = templeMemberService.addTempleMember(fieldsName, fieldsValue, idcard, checkcondition, master.getId(), process, operationType);
		if(!flag){
			message = "身份证输入有误";
			return "executeError";
		}
		message = "记录添加成功";
		return "executeSuccess";
	}
	
	//根据idcard
	public String showEdit(){
		MemberServiceImpl memberservice = new MemberServiceImpl();
		idcard = idcard.substring(0, 18);
		member = memberservice.getMemberByIdcard(idcard);
		return "showEditSuccess";
	}
	
	@SuppressWarnings("unchecked")
	public String edit(){
		MemberServiceImpl memberservice = new MemberServiceImpl();
		member = memberservice.getMemberByIdcard(idcard);
		process = Integer.parseInt(member.getValue("process"));
		String[] fieldsName;
		String[] fieldsValue;
		int size=0;
		List<Field> fields = (List<Field>) request.getSession().getAttribute("fields");
		//size代表相应阶段所需要的字段信息
		for(int i=0;i<fields.size();i++){
			if(fields.get(i).getStage() == process){
				size++;
			}
		}
		fieldsName = new String[size];	//字段的name
		fieldsValue = new String[size];	//字段的value
		//为正式团员时
		if(process == 4){
			size = fields.size();
			fieldsName = new String[size];
			fieldsValue = new String[size];
			for(int i=0,j=0;i<fields.size() && j<size;i++){
				String s = request.getParameter(fieldsName[j]);
				//不是‘备注’和'转出时间'字段
				if((!(fields.get(i).getName().equals("remark")))&&(!(fields.get(i).getName().equals("outtime")))){
					fieldsName[j] = fields.get(i).getName();
					if(s == null || s.equals("")){
						message = "所填信息不能为空";
						return "editError";
					}
					if(fields.get(i).getDefaultValue() != null){
						String ss = fields.get(i).getDefaultValue();
						if(!ss.equals(s)){
							message = "所填信息不符合默认值要求";
							return "editError";
						}
					}
				}
				fieldsValue[j] = s;
				j++;
			}
		}
		else{
			for(int i=0,j=0;i<fields.size() && j<size;i++){
				if(fields.get(i).getStage() == process){
					fieldsName[j] = fields.get(i).getName();
					String s = request.getParameter(fieldsName[j]);
					if(s == null || s.equals("")){
						message = "所填信息不能为空";
						return "editError";
					}
					fieldsValue[j] = s;
					j++;
				}
			}
		}
		Master master = (Master)request.getSession().getAttribute("master");
		templeMemberService = new TempleMemberServiceImpl();
		boolean flag = templeMemberService.addTempleMember(fieldsName, fieldsValue, idcard, 1, master.getId(), process, 1);
		if(!flag){
			message = "身份证填写错误";
			return "editError";
		}
		
		message = "记录添加成功";
		return "editSuccess";
	}
	
	public String delete(){
		Master master = (Master)request.getSession().getAttribute("master");
		templeMemberService = new TempleMemberServiceImpl();
		templeMemberService.addDeletedTempleMember(idcard,master.getId());
		return "deleteSuccess";
	}
}
